<template>
  <div>
    <el-dialog
      :title="dialogInfo.isAdd?'添加菜单':'编辑菜单'"
      :visible.sync="dialogInfo.isShow"
      :before-close="cancel"
      center
    >
      <!-- :model="menuInfo" :rules="rules"  看作整体绑定 -->
      <el-form :model="menuInfo" :rules="rules" ref="menuInfo">
        <el-form-item label="菜单名称" prop="title" :label-width="formLabelWidth">
          <el-input v-model="menuInfo.title" autocomplete="off"></el-input>
        </el-form-item>
        <!-- v-model="menuInfo.title"  prop  看作绑定到具体那个属性  -->
        <!-- el-form-item   用来写字 里面的prop 用来绑定验证    v-model在具体的输入框上面绑定双向-->
        <el-form-item label="上级菜单" prop="pid" :label-width="formLabelWidth">
          <el-select v-model="menuInfo.pid" placeholder="请选择">   
            <el-option label="顶级菜单" :value="0"></el-option>
              <!-- 下拉列表里面的数据 -->
            <el-option
              v-for="item in menuList"
              :key="item.id"   
              :label="item.title"
              :value="item.id"
            ></el-option>
                                <!--menuList是列表 id是编号，title是菜单名称 --> 
          </el-select>
        </el-form-item>
        <!-- el-form-item 为了让标签在一行 还要用它上面的prop去验证 label是Radio 的 value-->
        <el-form-item label="菜单类型" :label-width="formLabelWidth">
          <el-radio v-model="menuInfo.type" :label="1">目录</el-radio>
          <el-radio v-model="menuInfo.type" :label="2">菜单</el-radio>
        </el-form-item>

        <el-form-item label="菜单图标" v-if="menuInfo.type==1" :label-width="formLabelWidth">
          <el-select v-model="menuInfo.url" placeholder="请选择">   
           
              <!-- 下拉列表里面的数据 -->
            <el-option
              v-for="item in iconList"
              :key="item"   
              :label="item"
              :value="item"
            ></el-option>
                                <!--menuList是列表 id是编号，title是菜单名称 --> 
          </el-select>
        </el-form-item>
        <el-form-item label="菜单地址" v-else :label-width="formLabelWidth">
          <!-- <el-input v-model="menuInfo.url" autocomplete="off"></el-input> -->
           <el-select v-model="menuInfo.url" placeholder="请选择">   
           
              <!-- 下拉列表里面的数据 -->
            <el-option
              v-for="item in indexRoutes"
              :key="item.path"   
              :label="item.name"
              :value="item.path"
            ></el-option>
                                <!--menuList是列表 id是编号，title是菜单名称 --> 
          </el-select>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-switch  
            v-model="menuInfo.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" v-if="dialogInfo.isAdd" @click="addMenu">添加</el-button>
        <el-button type="primary" v-else @click="editMenu">编辑</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addMenu, getMenuList, getMenuInfo,postEditMenu} from "../../util/axios";
import { mapActions ,mapGetters} from "vuex";
//引入封装好的目录集合
import { indexRoutes } from "../../router";
export default {
  data() {
    return {
      menuInfo: {
        pid: 0, //上级分类编号
        title: "", //标题
        icon: "", //图标
        type: 1, //类型1目录2菜单
        url: "", //菜单地址
        status: 1 //状态1正常2禁用
      },
      rules: {
        title: [
          { required: true, message: "请输入菜单名称", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3到 5个字符", trigger: "blur" }
        ],
        pid: [{ required: true, message: "请选择上级菜单", trigger: "change" }]
      },
 iconList: [
        "el-icon-s-tools",
        "el-icon-setting",
        "el-icon-s-goods",
        "el-icon-goods"
      ],
      indexRoutes: indexRoutes,

      formLabelWidth: "120px",
      gridData: [
        {
          date: "2016-05-02",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1518 弄"
        },
        {
          date: "2016-05-04",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1518 弄"
        },
        {
          date: "2016-05-01",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1518 弄"
        },
        {
          date: "2016-05-03",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1518 弄"
        }
      ]
    };
  },
mounted(){


if(this.menuList.length==0){
  this.getMenuList()
}
console.log(this.indexRoutes);
},
  components: {},
  computed:{
    ...mapGetters({
      menuList:'menu/getMenuList' 
    })
  },
  methods: {
    ...mapActions({
      getMenuList: "menu/getMenuListAction"
    }),
    cancel() {
      this.$emit("cancel", false);
      this.reset();
      //   console.log(this.menuInfo);
    },
    
    addMenu() {
      this.$refs.menuInfo.validate(valid => {
        if (valid) {
          addMenu(this.menuInfo).then(res => {
            console.log(res, "响应");
            if (res.data.code == 200) {
              this.$message.success(res.data.msg);

              this.cancel();
              this.getMenuList();
            }else{
               this.$message.error(res.data.msg);
            }
          });
        } else {
          this.$message.error("验证失败");
              return false;
        }
      });
    },
    editMenu() {
      this.$refs.menuInfo.validate(valid => {
        if (valid) {
         postEditMenu(this.menuInfo).then(res => {
            console.log(res, "响应");
            if (res.data.code == 200) {
              this.$message.success(res.data.msg);

              this.cancel();
              this.getMenuList();
            }else{
               this.$message.error(res.data.msg);
            }
          });
        } else {
          this.$message.error("验证失败");
              return false;
        }
      });
    },
    reset() {
      this.menuInfo = {
        pid: 0, //上级分类编号
        title: "", //标题
        icon: "", //图标
        type: 1, //类型1目录2菜单
        url: "", //菜单地址
        status: 1 //状态1正常2禁用
      },
        this.$refs.menuInfo.resetFields(); //取消验证   ref="menuInfo"
    },         
    lookup(id){
            //  console.log(id,'menu通过ref方法传过来的');
          getMenuInfo({id})
           .then(res=>{
            if(res.data.code==200){
              console.log(res.data.list);
              this.menuInfo=res.data.list  
              this.menuInfo.id=id      //给编辑传一个id    //真实数据重新渲染   回显实现
           console.log(res);  //可以获取单条数据了
            }else{

              this.$message.error(res.data.msg)
            }
          
           
           })
    }
  },
  props: ["dialogInfo"]
};
</script>

<style lang="" scoped>
</style>
